/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.tms.service;

import java.util.List;

import com.jeeplus.modules.infor.entity.Goods;
import com.jeeplus.modules.infor.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.tms.entity.TransportPlanning;
import com.jeeplus.modules.tms.mapper.TransportPlanningMapper;
import com.jeeplus.modules.tms.entity.AdditionalCharges;
import com.jeeplus.modules.tms.mapper.AdditionalChargesMapper;
import com.jeeplus.modules.tms.entity.MaterialInformations;
import com.jeeplus.modules.tms.mapper.MaterialInformationsMapper;

/**
 * 委托单Service
 * @author 李超辉
 * @version 2017-12-04
 */
@Service
@Transactional(readOnly = true)
public class TransportPlanningService extends CrudService<TransportPlanningMapper, TransportPlanning> {

	@Autowired
	private AdditionalChargesMapper additionalChargesMapper;
	@Autowired
	private MaterialInformationsMapper materialInformationsMapper;
	@Autowired
	private GoodsService service;
	
	public TransportPlanning get(String id) {
		TransportPlanning transportPlanning = super.get(id);
//		transportPlanning.setAdditionalChargesList(additionalChargesMapper.findList(new AdditionalCharges(transportPlanning)));
		transportPlanning.setMaterialInformationsList(materialInformationsMapper.findList(new MaterialInformations(transportPlanning)));
		return transportPlanning;
	}
	
	public List<TransportPlanning> findList(TransportPlanning transportPlanning) {
		return super.findList(transportPlanning);
	}
	
	public Page<TransportPlanning> findPage(Page<TransportPlanning> page, TransportPlanning transportPlanning) {
		return super.findPage(page, transportPlanning);
	}
	
	@Transactional(readOnly = false)
	public void save(TransportPlanning transportPlanning) {
		super.save(transportPlanning);
		for (AdditionalCharges additionalCharges : transportPlanning.getAdditionalChargesList()){
			if (additionalCharges.getId() == null){
				continue;
			}
			if (AdditionalCharges.DEL_FLAG_NORMAL.equals(additionalCharges.getDelFlag())){
				if (StringUtils.isBlank(additionalCharges.getId())){
//					additionalCharges.setTransportPlanning(transportPlanning);
					additionalCharges.preInsert();
					additionalChargesMapper.insert(additionalCharges);
				}else{
					additionalCharges.preUpdate();
					additionalChargesMapper.update(additionalCharges);
				}
			}else{
				additionalChargesMapper.delete(additionalCharges);
			}
		}
		for (MaterialInformations materialInformations : transportPlanning.getMaterialInformationsList()){
			if (materialInformations.getId() == null){
				continue;
			}
			if (MaterialInformations.DEL_FLAG_NORMAL.equals(materialInformations.getDelFlag())){
				if (StringUtils.isBlank(materialInformations.getId())){
					materialInformations.setTransportPlanning(transportPlanning);
					Goods goods=service.get(materialInformations.getGoods().getId());
					materialInformations.setCoding(goods.getCode());
					materialInformations.setType(goods.getType().getName());
					materialInformations.setSpecification(goods.getSpecifications());
					materialInformations.setWeight((Double.parseDouble(goods.getGrossWeight())*Integer.parseInt(materialInformations.getGross()))+"");
					materialInformations.setVolume((Double.parseDouble(goods.getVolume())*Integer.parseInt(materialInformations.getGross()))+"");
					materialInformations.setRemain(materialInformations.getGross());
					materialInformations.setVehicleQuantity("0");
					materialInformations.preInsert();
					materialInformationsMapper.insert(materialInformations);
				}else{
					materialInformations.preUpdate();
					materialInformationsMapper.update(materialInformations);
				}
			}else{
				materialInformationsMapper.delete(materialInformations);
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(TransportPlanning transportPlanning) {
		super.delete(transportPlanning);
//		additionalChargesMapper.delete(new AdditionalCharges(transportPlanning));
		materialInformationsMapper.delete(new MaterialInformations(transportPlanning));
	}
	
}